package com.car.utils;

import com.car.domain.*;
import com.car.service.EmployeeService;
import com.car.service.PurchaseRecordService;
import com.car.service.StoreService;
import com.car.service.UserService;

import java.util.List;
import java.util.Scanner;

public class MenuUtils {
    private final static PurchaseRecordService purchaseRecordService = new PurchaseRecordService();
    private final static StoreService storeService = new StoreService();

    private final static EmployeeService employeeService = new EmployeeService();
    /**
     * 主页面
     */
    public static void mainMenu(Scanner scanner, UserService userService, Store store) throws Exception {
        int i;
        while (true) {
            System.out.println("======二手汽车系统======");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("0.退出");
            System.out.println("======================");
            System.out.println("请输入操作指令：");
            i = scanner.nextInt();
            if (i == 1) {
                // 登录
                User user = userService.login();
                if (user != null) {
                    System.out.println(user.getName() + "-登录成功");
                    // 用户菜单栏
                    userMenu(user, scanner, userService, store);
                } else {
                    System.out.println("登录失败");
                }
            } else if (i == 2) {
                //注册成功后 让他登录
                boolean isSuccess = userService.register();
                if (isSuccess) {
                    System.out.println("注册成功，请登录。");
                } else {
                    System.out.println("注册失败");
                }
            } else if (i == 3) {
                //员工登录
                Employee employee = employeeService.login(scanner);
                if (employee!=null){
                    employeeMenu(employee,scanner);
                }
            } else if (i == 0) {
                break;
            }else {
                System.out.println("输入错误，请重新输入：");
            }
        }
    }


    private static void employeeMenu(Employee employee, Scanner scanner) throws Exception {
        int n = 0;
        while (true) {
            System.out.println("=======员工菜单栏=======");
            System.out.println("1.查看商店汽车");
            System.out.println("2.上架汽车");
            System.out.println("3.查看购买记录");
            System.out.println("0.退出");
            System.out.println("======================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 1) {
                printCars(storeService.display());
            }else if (n==2){
                //上架汽车
                employeeService.addCar(employee,scanner);
            }else if (n==3){
                //查看购买记录
                List<PurchaseRecord> list= purchaseRecordService.list();
                for (PurchaseRecord pr : list){
                    System.out.println(pr);
                }
            }else if (n==0){
                break;
            }else {
                System.out.println("输入错误，请重新输入。");
            }
        }
    }

    /**
     * 用户菜单栏
     *
     * @param user 用户
     */
    private static void userMenu(User user, Scanner scanner, UserService userService, Store store) throws Exception {
        int n = 0;
        while (true) {
            System.out.println("=======用户菜单栏=======");
            System.out.println("1.查看商店汽车");
            System.out.println("2.查看自己拥有的汽车");
            System.out.println("3.购买汽车");
            System.out.println("4.查看购买记录");
            System.out.println("5.修改密码");
            System.out.println("6.修改用户名");
            System.out.println("0.退出");
            System.out.println("======================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 1) {
                printCars(store.getCarList());
            } else if (n == 2) {
                printCars(userService.ownCars(user));
            } else if (n == 3) {
                // 购买汽车
                userService.buyCar(user, scanner);
                store.setCarList(storeService.display());
            } else if (n == 4) {
                // 查看购买记录
                List<PurchaseRecord> purchaseRecords = purchaseRecordService.findByUserId(user.getId());
                System.out.println(purchaseRecords);
            } else if (n == 5) {
                // 修改密码
                userService.updatePassword(user, scanner);
            } else if (n == 6) {
                // 修改用户名
                userService.updateUsername(user, scanner);
            } else if (n == 0) {
                break;
            } else {
                System.out.println("输入错误，请重新输入！！！");
            }
        }
    }

    private static void printCars(List<Car> list) {
        System.out.println("id\tname\tbrand\tprice\ttype\tcolor\tcarNum\tstatus\temployeeId");
        for (Car car : list) {
            System.out.println(car.getId() + "\t" + car.getName() + "\t" + car.getBrand() + "\t" + car.getPrice()
                    + "\t" + car.getType() + "\t" + car.getColor() + "\t" + car.getCarNum() + car.getStatus() + "\t" + car.getEmployeeId());
        }
    }
}
